# Loading required package
library(haven)
### DOWNLOAD THE DATA FROM ROPER IPOLL (FOLLOW THE DOI LINKS, NAVIGATE TO THE "DOWNLOADS" TAB) ###
### THEN, LOAD AND PROCESS THE DATA ###

##################################
### SEVERE INTENSITY QUESTIONS ###
##################################
### CNN/ORC April 28, 2016 - May 1, 2016 (Garland) ###
# Load the data (Download from: https://doi.org/10.25940/ROPER-31095604)
cnn_orc_april_2016 <- read_spss("ipoll/31095604.por")

# Respondent ID
cnn_orc_april_2016$respondent_ID <- paste(1:length(cnn_orc_april_2016$ID), "cnn orc april 2016")

# Support for delay
# Omitting those with no opinion
cnn_orc_april_2016 <- cnn_orc_april_2016[which(cnn_orc_april_2016$Q36 %in% c(1,2)),]
cnn_orc_april_2016$support_delay <- ifelse(cnn_orc_april_2016$Q36 == 2,1,0)

# Copartisan of president - leaners as partisans
# Here, Democrats are copartisans
cnn_orc_april_2016$democrat <- ifelse(cnn_orc_april_2016$PARTY %in% c(1,2),1,0)
cnn_orc_april_2016$republican <- ifelse(cnn_orc_april_2016$PARTY %in% c(4,5),1,0)
cnn_orc_april_2016$copartisan_of_president <- ifelse(cnn_orc_april_2016$PARTY %in% c(1,2),1,0)

# Copartisan and outpartisan variables with leaners as independents
cnn_orc_april_2016$copartisan_of_president_leaners_as_independents <- ifelse(cnn_orc_april_2016$PARTY %in% c(1),1,0)
cnn_orc_april_2016$outpartisan_of_president_leaners_as_independents <- ifelse(cnn_orc_april_2016$PARTY %in% c(5),1,0)

# Support for nominee (survey has one of two support for nominee measures, other gets NA) 
cnn_orc_april_2016$want_the_nominee <- ifelse(cnn_orc_april_2016$Q35 %in% c(1),1,0)
cnn_orc_april_2016$positive_about_the_nominee <- rep(NA,length(cnn_orc_april_2016$ID))

# White
cnn_orc_april_2016$white <- ifelse(cnn_orc_april_2016$WHITE == 1,1,0)

# Male
cnn_orc_april_2016$male <- ifelse(cnn_orc_april_2016$SEX == 1,1,0)

# Survey indicator
cnn_orc_april_2016$survey <- rep("cnn orc april 2016",length(cnn_orc_april_2016$ID))

# Nomination indicator
cnn_orc_april_2016$nomination <- rep("garland",length(cnn_orc_april_2016$ID))

# Intensity indicator
cnn_orc_april_2016$intensity <- rep("severe",length(cnn_orc_april_2016$ID))

# Slimming down into format that can be combined with others
severe_cnn_orc_april_2016_slim <- cnn_orc_april_2016[,c("support_delay","democrat","republican","copartisan_of_president","want_the_nominee","positive_about_the_nominee",
                                                        "white","male","survey","nomination","intensity","copartisan_of_president_leaners_as_independents",
                                                        "outpartisan_of_president_leaners_as_independents","respondent_ID")]
rm(cnn_orc_april_2016)

### CNN/ORC March 17, 2016 - March 20, 2016 (Garland) ###
# Load the data (Download from: https://doi.org/10.25940/ROPER-31095603)
cnn_orc_march_2016 <- read_spss("ipoll/usorccnn2016-005.por")

# Respondent ID
cnn_orc_march_2016$respondent_ID <- paste(1:length(cnn_orc_march_2016$ID), "cnn orc march 2016")

# Support for delay
# Omitting those with no opinion
cnn_orc_march_2016 <- cnn_orc_march_2016[which(cnn_orc_march_2016$Q7 %in% c(1,2)),]
cnn_orc_march_2016$support_delay <- ifelse(cnn_orc_march_2016$Q7 == 2,1,0)

# Copartisan of president - leaners as partisans
# Here, Democrats are copartisans
cnn_orc_march_2016$democrat <- ifelse(cnn_orc_march_2016$PARTY %in% c(1,2),1,0)
cnn_orc_march_2016$republican <- ifelse(cnn_orc_march_2016$PARTY %in% c(4,5),1,0)
cnn_orc_march_2016$copartisan_of_president <- ifelse(cnn_orc_march_2016$PARTY %in% c(1,2),1,0)

# Copartisan and outpartisan variables with leaners as independents
cnn_orc_march_2016$copartisan_of_president_leaners_as_independents <- ifelse(cnn_orc_march_2016$PARTY %in% c(1),1,0)
cnn_orc_march_2016$outpartisan_of_president_leaners_as_independents <- ifelse(cnn_orc_march_2016$PARTY %in% c(5),1,0)

# Support for nominee
cnn_orc_march_2016$want_the_nominee <- ifelse(cnn_orc_march_2016$Q6 %in% c(1),1,0)
cnn_orc_march_2016$positive_about_the_nominee <- ifelse(cnn_orc_march_2016$Q10 %in% c(1,2),1,0)

# White
cnn_orc_march_2016$white <- ifelse(cnn_orc_march_2016$WHITE == 1,1,0)

# Male
cnn_orc_march_2016$male <- ifelse(cnn_orc_march_2016$SEX == 1,1,0)

# Survey indicator
cnn_orc_march_2016$survey <- rep("cnn orc march 2016",length(cnn_orc_march_2016$ID))

# Nomination indicator
cnn_orc_march_2016$nomination <- rep("garland",length(cnn_orc_march_2016$ID))

# Intensity indicator
cnn_orc_march_2016$intensity <- rep("severe",length(cnn_orc_march_2016$ID))

# Slimming down into format that can be combined with others
severe_cnn_orc_march_2016_slim <- cnn_orc_march_2016[,c("support_delay","democrat","republican","copartisan_of_president","want_the_nominee","positive_about_the_nominee",
                                                        "white","male","survey","nomination","intensity","copartisan_of_president_leaners_as_independents",
                                                        "outpartisan_of_president_leaners_as_independents","respondent_ID")]
rm(cnn_orc_march_2016)

### Monmouth March 17, 2016 - March 20, 2016 (Garland) ###
# Load the data (Download from: https://doi.org/10.25940/ROPER-31113904)
monmouth_march_2016 <- read_spss("ipoll/31113904.por")

# Respondent ID
monmouth_march_2016$respondent_ID <- paste(1:length(monmouth_march_2016$RESPID), "monmouth march 2016")

# Support for delay
# Omitting those with no opinion
monmouth_march_2016 <- monmouth_march_2016[which(monmouth_march_2016$QB5 %in% c(1,2)),]
monmouth_march_2016$support_delay <- ifelse(monmouth_march_2016$QB5 == 2,1,0)

# Copartisan of president - leaners as partisans
# Here, Democrats are copartisans
monmouth_march_2016$democrat <- ifelse((monmouth_march_2016$QD2 %in% c(2) | monmouth_march_2016$QD2A %in% c(2)),1,0)
monmouth_march_2016$republican <- ifelse((monmouth_march_2016$QD2 %in% c(1) | monmouth_march_2016$QD2A %in% c(1)),1,0)
monmouth_march_2016$copartisan_of_president <- ifelse((monmouth_march_2016$QD2 %in% c(2) | monmouth_march_2016$QD2A %in% c(2)),1,0)

# Copartisan and outpartisan variables with leaners as independents
monmouth_march_2016$copartisan_of_president_leaners_as_independents <- ifelse(monmouth_march_2016$QD2 %in% c(2),1,0)
monmouth_march_2016$outpartisan_of_president_leaners_as_independents <- ifelse(monmouth_march_2016$QD2 %in% c(1),1,0)

# Support for nominee (survey doesn't have it, so NA) 
monmouth_march_2016$want_the_nominee <- rep(NA, length(monmouth_march_2016$RESPID))
monmouth_march_2016$positive_about_the_nominee <- rep(NA, length(monmouth_march_2016$RESPID))

# White
monmouth_march_2016$white <- ifelse(monmouth_march_2016$QD8 == 1,1,0)

# Male
monmouth_march_2016$male <- ifelse(monmouth_march_2016$QD10 == 1,1,0)

# Survey indicator
monmouth_march_2016$survey <- rep("monmouth march 2016",length(monmouth_march_2016$RESPID))

# Nomination indicator
monmouth_march_2016$nomination <- rep("garland",length(monmouth_march_2016$RESPID))

# Intensity indicator
monmouth_march_2016$intensity <- rep("severe",length(monmouth_march_2016$RESPID))

# Slimming down into format that can be combined with others
severe_monmouth_march_2016_slim <- monmouth_march_2016[,c("support_delay","democrat","republican","copartisan_of_president","want_the_nominee","positive_about_the_nominee",
                                                          "white","male","survey","nomination","intensity","copartisan_of_president_leaners_as_independents",
                                                          "outpartisan_of_president_leaners_as_independents","respondent_ID")]
rm(monmouth_march_2016)

### CNN/ORC February 24, 2016 - February 27, 2016 (Scalia Vacancy) ###
# Load the data (Download from: https://doi.org/10.25940/ROPER-31095602)
# Also loaded below for a different question (moderate)
cnn_orc_february_2016 <- read_spss("ipoll/usorccnn2016-004.por")

# Respondent ID
cnn_orc_february_2016$respondent_ID <- paste(1:length(cnn_orc_february_2016$ID), "cnn orc february 2016")

# Support for delay
# Omitting those with no opinion
cnn_orc_february_2016 <- cnn_orc_february_2016[which(cnn_orc_february_2016$Q39A %in% c(1,2)),]
cnn_orc_february_2016$support_delay <- ifelse(cnn_orc_february_2016$Q39A == 2,1,0)

# Copartisan of president - leaners as partisans
# Here, Democrats are copartisans
cnn_orc_february_2016$democrat <- ifelse(cnn_orc_february_2016$PARTY %in% c(1,2),1,0)
cnn_orc_february_2016$republican <- ifelse(cnn_orc_february_2016$PARTY %in% c(4,5),1,0)
cnn_orc_february_2016$copartisan_of_president <- ifelse(cnn_orc_february_2016$PARTY %in% c(1,2),1,0)

# Copartisan and outpartisan variables with leaners as independents
cnn_orc_february_2016$copartisan_of_president_leaners_as_independents <- ifelse(cnn_orc_february_2016$PARTY %in% c(1),1,0)
cnn_orc_february_2016$outpartisan_of_president_leaners_as_independents <- ifelse(cnn_orc_february_2016$PARTY %in% c(5),1,0)

# Support for nominee (no nominee yet so survey doesn't have it, so NA) 
cnn_orc_february_2016$want_the_nominee <- rep(NA, length(cnn_orc_february_2016$ID))
cnn_orc_february_2016$positive_about_the_nominee <- rep(NA, length(cnn_orc_february_2016$ID))

# White
cnn_orc_february_2016$white <- ifelse(cnn_orc_february_2016$WHITE == 1,1,0)

# Male
cnn_orc_february_2016$male <- ifelse(cnn_orc_february_2016$SEX == 1,1,0)

# Survey indicator
cnn_orc_february_2016$survey <- rep("cnn orc february 2016",length(cnn_orc_february_2016$ID))

# Nomination indicator
cnn_orc_february_2016$nomination <- rep("scalia vacancy",length(cnn_orc_february_2016$ID))

# Intensity indicator
cnn_orc_february_2016$intensity <- rep("severe",length(cnn_orc_february_2016$ID))

# Slimming down into format that can be combined with others
severe_cnn_orc_february_2016_slim <- cnn_orc_february_2016[,c("support_delay","democrat","republican","copartisan_of_president","want_the_nominee","positive_about_the_nominee",
                                                              "white","male","survey","nomination","intensity","copartisan_of_president_leaners_as_independents",
                                                              "outpartisan_of_president_leaners_as_independents","respondent_ID")]
rm(cnn_orc_february_2016)

####################################
### MODERATE INTENSITY QUESTIONS ###
####################################
### CBS News/New York Times May 13, 2016 - May 17, 2016 (Garland) ###
# Load the data (Download from: https://doi.org/10.25940/ROPER-31091613)
cbsnyt_may_2016 <- read_spss("ipoll/uscbsnyt2016-0517.por")

# Respondent ID
cbsnyt_may_2016$respondent_ID <- paste(1:length(cbsnyt_may_2016$ID), "cbs nyt may 2016")

# Support for delay
# Omitting those with no opinion
cbsnyt_may_2016 <- cbsnyt_may_2016[which(cbsnyt_may_2016$CNFRMGRL %in% c(1,2)),]
cbsnyt_may_2016$support_delay <- ifelse(cbsnyt_may_2016$CNFRMGRL == 2,1,0)

# Copartisan of president (for this survey this is the party variable available -- leaners are treated as independents)
# Here, Democrats are copartisans
cbsnyt_may_2016$democrat <- ifelse(cbsnyt_may_2016$PRTY == 2,1,0)
cbsnyt_may_2016$republican <- ifelse(cbsnyt_may_2016$PRTY == 1,1,0) 
cbsnyt_may_2016$copartisan_of_president <- ifelse(cbsnyt_may_2016$PRTY == 2,1,0)

# Copartisan and outpartisan variables with leaners as independents (same as above because of just this variable available)
cbsnyt_may_2016$copartisan_of_president_leaners_as_independents <- ifelse(cbsnyt_may_2016$PRTY == 2,1,0)
cbsnyt_may_2016$outpartisan_of_president_leaners_as_independents <- ifelse(cbsnyt_may_2016$PRTY == 1,1,0)

# Support for nominee (survey doesn't have it, so NA) 
cbsnyt_may_2016$want_the_nominee <- rep(NA, length(cbsnyt_may_2016$ID))
cbsnyt_may_2016$positive_about_the_nominee <- rep(NA, length(cbsnyt_may_2016$ID))

# White
cbsnyt_may_2016$white <- ifelse(cbsnyt_may_2016$RACE == 1,1,0)

# Male
cbsnyt_may_2016$male <- ifelse(cbsnyt_may_2016$SEX == 1,1,0)

# Survey indicator
cbsnyt_may_2016$survey <- rep("cbs nyt may 2016",length(cbsnyt_may_2016$ID))

# Nomination indicator
cbsnyt_may_2016$nomination <- rep("garland",length(cbsnyt_may_2016$ID))

# Intensity indicator
cbsnyt_may_2016$intensity <- rep("moderate",length(cbsnyt_may_2016$ID))

# Slimming down into format that can be combined with others
moderate_cbsnyt_may_2016_slim <- cbsnyt_may_2016[,c("support_delay","democrat","republican","copartisan_of_president","want_the_nominee","positive_about_the_nominee",
                                                    "white","male","survey","nomination","intensity","copartisan_of_president_leaners_as_independents",
                                                    "outpartisan_of_president_leaners_as_independents","respondent_ID")]
rm(cbsnyt_may_2016)

### CBS News/New York Times March 17, 2016 - March 20, 2016 (Garland) ###
# Load the data (Download from: https://doi.org/10.25940/ROPER-31091612)
cbsnyt_march_2016 <- read_spss("ipoll/uscbsnyt2016-0320.por")

# Respondent ID
cbsnyt_march_2016$respondent_ID <- paste(1:length(cbsnyt_march_2016$ID), "cbs nyt march 2016")

# Support for delay
# Omitting those with no opinion
cbsnyt_march_2016 <- cbsnyt_march_2016[which(cbsnyt_march_2016$CNFRMGRL %in% c(1,2)),]
cbsnyt_march_2016$support_delay <- ifelse(cbsnyt_march_2016$CNFRMGRL == 2,1,0)

# Copartisan of president  (for this survey this is the party variable available -- leaners are treated as independents)
# Here, Democrats are copartisans
cbsnyt_march_2016$democrat <- ifelse(cbsnyt_march_2016$PRTY == 2,1,0)
cbsnyt_march_2016$republican <- ifelse(cbsnyt_march_2016$PRTY == 1,1,0) 
cbsnyt_march_2016$copartisan_of_president <- ifelse(cbsnyt_march_2016$PRTY == 2,1,0)

# Copartisan and outpartisan variables with leaners as independents (same as above because of just this variable available)
cbsnyt_march_2016$copartisan_of_president_leaners_as_independents <- ifelse(cbsnyt_march_2016$PRTY == 2,1,0)
cbsnyt_march_2016$outpartisan_of_president_leaners_as_independents <- ifelse(cbsnyt_march_2016$PRTY == 1,1,0)

# Support for nominee (survey has one of two support for nominee measures, other gets NA) 
cbsnyt_march_2016$want_the_nominee <- rep(NA, length(cbsnyt_march_2016$ID))
cbsnyt_march_2016$positive_about_the_nominee <- ifelse(cbsnyt_march_2016$GRLNDFAV %in% c(1),1,0)

# White
cbsnyt_march_2016$white <- ifelse(cbsnyt_march_2016$RACE == 1,1,0)

# Male
cbsnyt_march_2016$male <- ifelse(cbsnyt_march_2016$SEX == 1,1,0)

# Survey indicator
cbsnyt_march_2016$survey <- rep("cbs nyt march 2016",length(cbsnyt_march_2016$ID))

# Nomination indicator
cbsnyt_march_2016$nomination <- rep("garland",length(cbsnyt_march_2016$ID))

# Intensity indicator
cbsnyt_march_2016$intensity <- rep("moderate",length(cbsnyt_march_2016$ID))

# Slimming down into format that can be combined with others
moderate_cbsnyt_march_2016_slim <- cbsnyt_march_2016[,c("support_delay","democrat","republican","copartisan_of_president","want_the_nominee","positive_about_the_nominee",
                                                        "white","male","survey","nomination","intensity","copartisan_of_president_leaners_as_independents",
                                                        "outpartisan_of_president_leaners_as_independents","respondent_ID")]
rm(cbsnyt_march_2016)

### CNN/ORC January 31, 2017 - February 2, 2017 (Gorusch) ###
# Load the data (Download from: https://doi.org/10.25940/ROPER-31102946)
cnn_orc_january_2017 <- read_spss("ipoll/31102946.por")

# Respondent ID
cnn_orc_january_2017$respondent_ID <- paste(1:length(cnn_orc_january_2017$ID), "cnn orc january 2017")

# Support for delay
# Omitting those with no opinion
cnn_orc_january_2017 <- cnn_orc_january_2017[which(cnn_orc_january_2017$Q16 %in% c(1,2)),]
cnn_orc_january_2017$support_delay <- ifelse(cnn_orc_january_2017$Q16 == 1,1,0)

# Copartisan of president - leaners as partisans
# Here, Republicans are copartisans
cnn_orc_january_2017$democrat <- ifelse(cnn_orc_january_2017$PARTY %in% c(1,2),1,0)
cnn_orc_january_2017$republican <- ifelse(cnn_orc_january_2017$PARTY %in% c(4,5),1,0)
cnn_orc_january_2017$copartisan_of_president <- ifelse(cnn_orc_january_2017$PARTY %in% c(4,5),1,0)

# Copartisan and outpartisan variables with leaners as independents
cnn_orc_january_2017$copartisan_of_president_leaners_as_independents <- ifelse(cnn_orc_january_2017$PARTY %in% c(5),1,0)
cnn_orc_january_2017$outpartisan_of_president_leaners_as_independents <- ifelse(cnn_orc_january_2017$PARTY %in% c(1),1,0)

# Support for nominee 
cnn_orc_january_2017$want_the_nominee <- ifelse(cnn_orc_january_2017$Q13 == 1,1,0)
cnn_orc_january_2017$positive_about_the_nominee <- ifelse(cnn_orc_january_2017$Q14 %in% c(1,2),1,0)

# White
cnn_orc_january_2017$white <- ifelse(cnn_orc_january_2017$WHITE == 1,1,0)

# Male
cnn_orc_january_2017$male <- ifelse(cnn_orc_january_2017$SEX == 1,1,0)

# Survey indicator
cnn_orc_january_2017$survey <- rep("cnn orc january 2017",length(cnn_orc_january_2017$ID))

# Nomination indicator
cnn_orc_january_2017$nomination <- rep("gorsuch",length(cnn_orc_january_2017$ID))

# Intensity indicator
cnn_orc_january_2017$intensity <- rep("moderate",length(cnn_orc_january_2017$ID))

# Slimming down into format that can be combined with others
moderate_cnn_orc_january_2017_slim <- cnn_orc_january_2017[,c("support_delay","democrat","republican","copartisan_of_president","want_the_nominee","positive_about_the_nominee",
                                                              "white","male","survey","nomination","intensity","copartisan_of_president_leaners_as_independents",
                                                              "outpartisan_of_president_leaners_as_independents","respondent_ID")]
rm(cnn_orc_january_2017)

### Gallup/CNN/USA Today November 1, 2005 (Alito) ###
# Load the data (Download from: https://doi.org/10.25940/ROPER-31088631)
cnn_usa_november_2005 <- read_spss("ipoll/g200553.por")

# Respondent ID
cnn_usa_november_2005$respondent_ID <- paste(1:length(cnn_usa_november_2005$ID), "cnn usa november 2005")

# Support for delay
# Omitting those with no opinion
cnn_usa_november_2005 <- cnn_usa_november_2005[which(cnn_usa_november_2005$Q7 %in% c(1,2)),]
cnn_usa_november_2005$support_delay <- ifelse(cnn_usa_november_2005$Q7 == 1,1,0)

# Copartisan of president - leaners as partisans
# Here, Republicans are copartisans
cnn_usa_november_2005$democrat <- ifelse(cnn_usa_november_2005$PARTY %in% c(4,5),1,0)
cnn_usa_november_2005$republican <- ifelse(cnn_usa_november_2005$PARTY %in% c(1,2),1,0)
cnn_usa_november_2005$copartisan_of_president <- ifelse(cnn_usa_november_2005$PARTY %in% c(1,2),1,0)

# Copartisan and outpartisan variables with leaners as independents
cnn_usa_november_2005$copartisan_of_president_leaners_as_independents <- ifelse(cnn_usa_november_2005$PARTY %in% c(1),1,0)
cnn_usa_november_2005$outpartisan_of_president_leaners_as_independents <- ifelse(cnn_usa_november_2005$PARTY %in% c(5),1,0)

# Support for nominee (survey has one of two support for nominee measures, other gets NA)
cnn_usa_november_2005$want_the_nominee <- rep(NA, length(cnn_usa_november_2005$ID))
cnn_usa_november_2005$positive_about_the_nominee <- ifelse(cnn_usa_november_2005$Q2 %in% c(1,2),1,0)

# White
cnn_usa_november_2005$white <- ifelse(cnn_usa_november_2005$RACE == 1,1,0)

# Male
cnn_usa_november_2005$male <- ifelse(cnn_usa_november_2005$D1 == 1,1,0)

# Survey indicator
cnn_usa_november_2005$survey <- rep("cnn usa november 2005",length(cnn_usa_november_2005$ID))

# Nomination indicator
cnn_usa_november_2005$nomination <- rep("alito",length(cnn_usa_november_2005$ID))

# Intensity indicator
cnn_usa_november_2005$intensity <- rep("moderate",length(cnn_usa_november_2005$ID))

# Slimming down into format that can be combined with others
moderate_cnn_usa_november_2005_slim <- cnn_usa_november_2005[,c("support_delay","democrat","republican","copartisan_of_president","want_the_nominee","positive_about_the_nominee",
                                                                "white","male","survey","nomination","intensity","copartisan_of_president_leaners_as_independents",
                                                                "outpartisan_of_president_leaners_as_independents","respondent_ID")]
rm(cnn_usa_november_2005)

### Gallup/CNN/USA Today January 20, 2006 - January 22, 2006 (Alito) ###
# Load the data (Download from: https://doi.org/10.25940/ROPER-31088639)
cnn_usa_january_2006 <- read_spss("ipoll/g200603.por")

# Respondent ID
cnn_usa_january_2006$respondent_ID <- paste(1:length(cnn_usa_january_2006$ID), "cnn usa january 2006")

# Support for delay
# Omitting those with no opinion
cnn_usa_january_2006 <- cnn_usa_january_2006[which(cnn_usa_january_2006$Q29 %in% c(1,2)),]
cnn_usa_january_2006$support_delay <- ifelse(cnn_usa_january_2006$Q29 == 1,1,0)

# Copartisan of president - leaners as partisans
# Here, Republicans are copartisans
cnn_usa_january_2006$democrat <- ifelse(cnn_usa_january_2006$PARTY %in% c(4,5),1,0)
cnn_usa_january_2006$republican <- ifelse(cnn_usa_january_2006$PARTY %in% c(1,2),1,0)
cnn_usa_january_2006$copartisan_of_president <- ifelse(cnn_usa_january_2006$PARTY %in% c(1,2),1,0)

# Copartisan and outpartisan variables with leaners as independents
cnn_usa_january_2006$copartisan_of_president_leaners_as_independents <- ifelse(cnn_usa_january_2006$PARTY %in% c(1),1,0)
cnn_usa_january_2006$outpartisan_of_president_leaners_as_independents <- ifelse(cnn_usa_january_2006$PARTY %in% c(5),1,0)

# Support for nominee (survey has one of two support for nominee measures, other gets NA)
cnn_usa_january_2006$want_the_nominee <- ifelse(cnn_usa_january_2006$Q28 %in% c(1),1,0)
cnn_usa_january_2006$positive_about_the_nominee <- rep(NA, length(cnn_usa_january_2006$ID))

# White
cnn_usa_january_2006$white <- ifelse(cnn_usa_january_2006$RACE == 1,1,0)

# Male
cnn_usa_january_2006$male <- ifelse(cnn_usa_january_2006$S2 == 1,1,0)

# Survey indicator
cnn_usa_january_2006$survey <- rep("cnn usa january 2006",length(cnn_usa_january_2006$ID))

# Nomination indicator
cnn_usa_january_2006$nomination <- rep("alito",length(cnn_usa_january_2006$ID))

# Intensity indicator
cnn_usa_january_2006$intensity <- rep("moderate",length(cnn_usa_january_2006$ID))

# Slimming down into format that can be combined with others
moderate_cnn_usa_january_2006_slim <- cnn_usa_january_2006[,c("support_delay","democrat","republican","copartisan_of_president","want_the_nominee","positive_about_the_nominee",
                                                              "white","male","survey","nomination","intensity","copartisan_of_president_leaners_as_independents",
                                                              "outpartisan_of_president_leaners_as_independents","respondent_ID")]
rm(cnn_usa_january_2006)

### CNN/ORC February 24, 2016 - February 27, 2016 (Scalia Vacancy) ###
# Load the data (Download from: https://doi.org/10.25940/ROPER-31095602)
# Also loaded above for a different question (severe)
cnn_orc_february_2016 <- read_spss("ipoll/usorccnn2016-004.por")

# Respondent ID
cnn_orc_february_2016$respondent_ID <- paste(1:length(cnn_orc_february_2016$ID), "cnn orc february 2016")

# Support for delay
# Omitting those with no opinion
cnn_orc_february_2016 <- cnn_orc_february_2016[which(cnn_orc_february_2016$Q41 %in% c(1,2)),]
cnn_orc_february_2016$support_delay <- ifelse(cnn_orc_february_2016$Q41 == 1,1,0)

# Copartisan of president - leaners as partisans
# Here, Democrats are copartisans
cnn_orc_february_2016$democrat <- ifelse(cnn_orc_february_2016$PARTY %in% c(1,2),1,0)
cnn_orc_february_2016$republican <- ifelse(cnn_orc_february_2016$PARTY %in% c(4,5),1,0)
cnn_orc_february_2016$copartisan_of_president <- ifelse(cnn_orc_february_2016$PARTY %in% c(1,2),1,0)

# Copartisan and outpartisan variables with leaners as independents
cnn_orc_february_2016$copartisan_of_president_leaners_as_independents <- ifelse(cnn_orc_february_2016$PARTY %in% c(1),1,0)
cnn_orc_february_2016$outpartisan_of_president_leaners_as_independents <- ifelse(cnn_orc_february_2016$PARTY %in% c(5),1,0)

# Support for nominee (no nominee yet so survey doesn't have it, so NA) 
cnn_orc_february_2016$want_the_nominee <- rep(NA, length(cnn_orc_february_2016$ID))
cnn_orc_february_2016$positive_about_the_nominee <- rep(NA, length(cnn_orc_february_2016$ID))

# White
cnn_orc_february_2016$white <- ifelse(cnn_orc_february_2016$WHITE == 1,1,0)

# Male
cnn_orc_february_2016$male <- ifelse(cnn_orc_february_2016$SEX == 1,1,0)

# Survey indicator
cnn_orc_february_2016$survey <- rep("cnn orc february 2016",length(cnn_orc_february_2016$ID))

# Nomination indicator
cnn_orc_february_2016$nomination <- rep("scalia vacancy",length(cnn_orc_february_2016$ID))

# Intensity indicator
cnn_orc_february_2016$intensity <- rep("moderate",length(cnn_orc_february_2016$ID))

# Slimming down into format that can be combined with others
moderate_cnn_orc_february_2016_slim <- cnn_orc_february_2016[,c("support_delay","democrat","republican","copartisan_of_president","want_the_nominee","positive_about_the_nominee",
                                                                "white","male","survey","nomination","intensity","copartisan_of_president_leaners_as_independents",
                                                                "outpartisan_of_president_leaners_as_independents","respondent_ID")]
rm(cnn_orc_february_2016)

################################
### WEAK INTENSITY QUESTIONS ###
################################
### CNN August 9, 2018 - August 12, 2018 (Kavanaugh) ###
# Load the data (Download from: https://doi.org/10.25940/ROPER-31115414)
cnn_august_2018 <- read_spss("ipoll/31115414.por")

# Respondent ID
cnn_august_2018$respondent_ID <- paste(1:length(cnn_august_2018$ID), "cnn august 2018")

# Support for delay
# Omitting those with no opinion
cnn_august_2018 <- cnn_august_2018[which(cnn_august_2018$Q13 %in% c(1,2)),]
cnn_august_2018$support_delay <- ifelse(cnn_august_2018$Q13 == 1,1,0)

# Copartisan of president - leaners as partisans
# Here, Republicans are copartisans
cnn_august_2018$democrat <- ifelse(cnn_august_2018$PARTLEAN == 1,1,0)
cnn_august_2018$republican <- ifelse(cnn_august_2018$PARTLEAN == 3,1,0) 
cnn_august_2018$copartisan_of_president <- ifelse(cnn_august_2018$PARTLEAN == 3,1,0)  

# Copartisan and outpartisan variables with leaners as independents
cnn_august_2018$copartisan_of_president_leaners_as_independents <- ifelse(cnn_august_2018$PARTY %in% c(5),1,0)
cnn_august_2018$outpartisan_of_president_leaners_as_independents <- ifelse(cnn_august_2018$PARTY %in% c(1),1,0)

# Support for nominee
cnn_august_2018$want_the_nominee <- ifelse(cnn_august_2018$Q9 == 1,1,0)
cnn_august_2018$positive_about_the_nominee <- ifelse(cnn_august_2018$Q10 %in% c(1,2),1,0)

# White
cnn_august_2018$white <- ifelse(cnn_august_2018$WHITE == 1,1,0)

# Male
cnn_august_2018$male <- ifelse(cnn_august_2018$SEX == 1,1,0)

# Survey indicator
cnn_august_2018$survey <- rep("cnn august 2018",length(cnn_august_2018$ID))

# Nomination indicator
cnn_august_2018$nomination <- rep("kavanaugh",length(cnn_august_2018$ID))

# Intensity indicator
cnn_august_2018$intensity <- rep("weak",length(cnn_august_2018$ID))

# Slimming down into format that can be combined with others
weak_cnn_august_2008_slim <- cnn_august_2018[,c("support_delay","democrat","republican","copartisan_of_president","want_the_nominee","positive_about_the_nominee",
                                                "white","male","survey","nomination","intensity","copartisan_of_president_leaners_as_independents",
                                                "outpartisan_of_president_leaners_as_independents","respondent_ID")]
rm(cnn_august_2018)

### Hart-McInturff/NBC/WSJ September 9, 2005 - September 12, 2005 (Roberts) ###
# Load the data (Download from: https://doi.org/10.25940/ROPER-31094844)
nbc_wsj_sept_2005 <- read_spss("ipoll/6056.por")

# Respondent ID
nbc_wsj_sept_2005$respondent_ID <- paste(1:length(nbc_wsj_sept_2005$SEX), "nbc wsj sept 2005")

# Support for delay
# Omitting those with no opinion
nbc_wsj_sept_2005 <- nbc_wsj_sept_2005[which(nbc_wsj_sept_2005$Q27 %in% c(1,2)),]
nbc_wsj_sept_2005$support_delay <- ifelse(nbc_wsj_sept_2005$Q27 == 1,1,0)

# Copartisan of president - leaners as partisans
# Here, Republicans are copartisans
nbc_wsj_sept_2005$democrat <- ifelse(nbc_wsj_sept_2005$QF5 %in% c(1,2,3),1,0)
nbc_wsj_sept_2005$republican <- ifelse(nbc_wsj_sept_2005$QF5 %in% c(5,6,7),1,0)
nbc_wsj_sept_2005$copartisan_of_president <- ifelse(nbc_wsj_sept_2005$QF5 %in% c(5,6,7),1,0) 

# Copartisan and outpartisan variables with leaners as independents
nbc_wsj_sept_2005$copartisan_of_president_leaners_as_independents <- ifelse(nbc_wsj_sept_2005$QF5 %in% c(6,7),1,0)
nbc_wsj_sept_2005$outpartisan_of_president_leaners_as_independents <- ifelse(nbc_wsj_sept_2005$QF5 %in% c(1,2),1,0)

# Support for nominee (survey has one of two support for nominee measures, other gets NA)
nbc_wsj_sept_2005$want_the_nominee <- ifelse(nbc_wsj_sept_2005$Q24 %in% c(1,2),1,0)
nbc_wsj_sept_2005$positive_about_the_nominee <- rep(NA, length(nbc_wsj_sept_2005$SEX))

# White
nbc_wsj_sept_2005$white <- ifelse(nbc_wsj_sept_2005$Q2C == 1,1,0)

# Male
nbc_wsj_sept_2005$male <- ifelse(nbc_wsj_sept_2005$SEX == 1,1,0)

# Survey indicator
nbc_wsj_sept_2005$survey <- rep("nbc wsj sept 2005",length(nbc_wsj_sept_2005$SEX))

# Nomination indicator
nbc_wsj_sept_2005$nomination <- rep("roberts",length(nbc_wsj_sept_2005$SEX))

# Intensity indicator
nbc_wsj_sept_2005$intensity <- rep("weak",length(nbc_wsj_sept_2005$SEX))

# Slimming down into format that can be combined with others
weak_nbc_wsj_sept_2005_slim <- nbc_wsj_sept_2005[,c("support_delay","democrat","republican","copartisan_of_president","want_the_nominee","positive_about_the_nominee",
                                                    "white","male","survey","nomination","intensity","copartisan_of_president_leaners_as_independents",
                                                    "outpartisan_of_president_leaners_as_independents","respondent_ID")]
rm(nbc_wsj_sept_2005)

##################################
### COMBINING ALL OF THE POLLS ###
##################################
ipoll_individual_dataset <- rbind(severe_cnn_orc_april_2016_slim, severe_cnn_orc_march_2016_slim, severe_monmouth_march_2016_slim, severe_cnn_orc_february_2016_slim,
                                  moderate_cbsnyt_may_2016_slim, moderate_cbsnyt_march_2016_slim, moderate_cnn_orc_january_2017_slim, moderate_cnn_usa_november_2005_slim, 
                                  moderate_cnn_usa_january_2006_slim, moderate_cnn_orc_february_2016_slim, weak_cnn_august_2008_slim, weak_nbc_wsj_sept_2005_slim)

# Outpartisan of president variable (treating leaners as partisans)
ipoll_individual_dataset$outpartisan_of_president <- ifelse(((ipoll_individual_dataset$nomination %in% c("alito","gorsuch","kavanaugh","roberts") & ipoll_individual_dataset$democrat == 1) | 
                                                               (ipoll_individual_dataset$nomination %in% c("scalia vacancy","garland") & ipoll_individual_dataset$republican == 1)), 1, 0)

# Intensity as factor, weak as baseline
ipoll_individual_dataset$intensity <- factor(ipoll_individual_dataset$intensity, levels=c("weak","moderate","severe"))

# President fixed effects
president.coding <- rep(NA, length(ipoll_individual_dataset$support_delay))
president.coding <- ifelse(ipoll_individual_dataset$nomination %in% c("alito","roberts"), "bush", president.coding)
president.coding <- ifelse(ipoll_individual_dataset$nomination %in% c("kavanaugh","gorsuch"), "trump", president.coding)
president.coding <- ifelse(ipoll_individual_dataset$nomination %in% c("garland","scalia vacancy"), "obama", president.coding)

ipoll_individual_dataset$president <- factor(president.coding, levels=c("bush","obama","trump"))

save(ipoll_individual_dataset, file="ipoll_individual_surveys.RData")
